<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Hero - Admin</title>
    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
    <style>
        .hero-list img {
            width: 50px;
        }
    </style>
</head>

<body>
    <header>
        <div class="page-header container">
            <h1>王者荣耀
                <small>英雄管理器</small>
            </h1>
        </div>
    </header>
    <div class="container hero-list">
        <form action="/add" method="POST" enctype="multipart/form-data">
            <table class="table table-hover">
                <tr>
                    <td>姓名：</td>
                    <td>
                        <input type="text" name="userName" class="userName" id="userName">
                    </td>
                </tr>
                <tr>
                    <td>性别：</td>
                    <td>
                        <input id="nan" name="gender" value="男" type="radio">
                        <label for="nan">男</label>
                        <input name="gender" value="女" type="radio" id="nv">
                        <label for="nv">女</label>
                    </td>
                </tr>
                <tr>
                    <td>头像：</td>
                    <td>
                        <img src="" alt="" id="photo" last="">
                        <input type="file" name="img" id="img">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" id="sub" value="新增">
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
<script src="/node_modules/jquery/dist/jquery.min.js"></script>

</html>
<script>
    $("#img").on("change", function (e) {
        var formData = new FormData();
        var myFile = this.files[0];
        formData.append("myPic", myFile);
        formData.append("last", $("#photo").attr("last"));
        $.ajax({
            url: "/postupload",
            type: "POST",
            data: formData,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    $("#photo").attr("src", "/upload/" + result.img);
                    $("#photo").attr("last", result.img);
                }
            }
        })
    })
    $("#sub").on("click", function () {
        var formData = new FormData();
        var userName = $("#userName").val();
        var gender = $("input[type=radio]:checked").val();
        var img = $("#photo").attr("last");
        formData.append("name", userName);
        formData.append("gender", gender);
        formData.append("img", img);
        $.ajax({
            url: "/add",
            type: "POST",
            data: formData,
            beforeSend: function () {
                if (userName == "") {
                    return false;
                }
            },
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    alert(result.msg);
                    window.location.href = "/";
                }
            }
        })
    })
</script>